package file_io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class FileIOMain {

    private ArrayList outputList = new ArrayList();

    FileIOMain()
    {
        outputList.add("NO.:12345");
        outputList.add("Name:zhangsan");
        outputList.add("love sports");
        for (int i = 0; i < 1000; i++)
        {
            String morning ="Good Morning";
            outputList.add(morning + i);
        }
    }

    public void readFile(String file_path) throws IOException
    {
        File file;
        FileReader fr;
        BufferedReader br ;

        file = new File(file_path);
        if (!file.exists()){
            System.out.println("\""+file_path+"\" does not exsit!");
            return;
        }

		/*
		1.FileReader鏄敱java.io.InputStreamReade鎵╁睍鏉ョ殑锛屾槸閽堝鏂囦欢璇诲彇鐨勶紝鍙兘杩涜搴曞眰鐨勫瓧鑺傛搷浣溿��
		BufferedReader鐢盧eader绫绘墿灞曡�屾潵锛屾彁渚涢�氱敤鐨勭紦鍐叉柟寮忔枃鏈鍙栵紝鑰屼笖鎻愪緵浜嗗緢瀹炵敤鐨剅eadLine锛�
		璇诲彇鍒嗚鏂囨湰寰堥�傚悎锛孊ufferedReader鏄拡瀵筊eader鐨勶紝涓嶇洿鎺ラ拡瀵规枃浠讹紝涔熶笉鏄彧閽堝鏂囦欢璇诲彇銆�
		2.FileReader鏄敤鏉ヨ鏂囦欢鐨勭被锛岃�孊ufferReader鏄皢IO娴佽浆鎹负Buffer浠ユ彁楂樼▼搴忕殑澶勭悊閫熷害銆�
		*/
        fr = new FileReader(file);
        br = new BufferedReader(fr);

		/*浣滀笟锛氭敼鍐欎互涓嬩唬鐮侊紝浣跨敤寰幆璇诲彇鏂囦欢鍏ㄩ儴鍐呭骞舵墦鍗板埌灞忓箷锛屾彁绀猴細闇�瑕佷娇鐢˙ufferedReader.ready()鏂规硶锛屽垽鏂枃浠惰闂槸鍚﹀埌鏂囦欢鏈熬*/

        while(br.ready())
        {
            String lineText;
            lineText = br.readLine();
            System.out.println(lineText);
        }
		/*浣滀笟end*/

        br.close();
    }

    public void writeFile(String file_path) throws IOException
    {
        File file;
        FileWriter fw;
        PrintWriter pw;

        file = new File(file_path);
        if (!file.exists()){
            file.createNewFile();
        }
        fw = new FileWriter(file);
        pw = new PrintWriter(fw);

		/*浣滀笟锛氭敼鍐欎互涓嬩唬鐮侊紝浣跨敤寰幆杈撳嚭FileIOMain.outputList鑷虫枃浠�*/

        for (Object lineText : outputList) {
            pw.println(lineText);
        }
		/*浣滀笟end*/
        pw.close();
    }

    public static void main(String[] args) throws IOException {
        FileIOMain io = new FileIOMain();
        io.readFile("C:\\Users\\COS\\Desktop\\hw3_input.txt");
        io.writeFile("C:\\\\Users\\\\COS\\\\Desktop\\\\hw3_output.txt");
    }

}